package com.ja.openchat.ui.contact.search;

import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.ja.openchat.views.UserAvatarView;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.DateHelper;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class PublicSearchSectionAdapter extends BaseSectionQuickAdapter<SearchSection, BaseViewHolder> implements LoadMoreModule {
    public PublicSearchSectionAdapter(int sectionHeadResId, @Nullable List<SearchSection> data) {
        super(sectionHeadResId, data);
        addItemType(SearchSection.SearchSectionType_MyContacts, R.layout.adapter_contact_list);
        addItemType(SearchSection.SearchSectionType_PublicContacts, R.layout.adapter_contact_list);
        addItemType(SearchSection.SearchSectionType_MyGroups, R.layout.adapter_contact_list);
        addItemType(SearchSection.SearchSectionType_PublicGroups, R.layout.adapter_contact_list);
        addItemType(SearchSection.SearchSectionType_MyChannels, R.layout.adapter_contact_list);
        addItemType(SearchSection.SearchSectionType_PublicChannels, R.layout.adapter_contact_list);
        addItemType(SearchSection.SearchSectionType_HisMsgs, R.layout.item_search_message_list);
    }

    @Override
    protected void convertHeader(@NotNull BaseViewHolder helper, @NotNull SearchSection section) {
        helper.setText(R.id.iv_title, ((SearchSectionInfo) section.getObject()).title);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, SearchSection section) {
        Object obj = section.getObject();
        if (obj instanceof UserInfo) {
            UserInfo user = (UserInfo) obj;
            helper.setText(R.id.iv_title, user.displayName());
            UserAvatarView avatarView = helper.getView(R.id.iv_icon);
            avatarView.setTextSize(17);
            avatarView.showUser(user);
        }
        if (obj instanceof MessageInfo) {
            MessageInfo messageInfo = (MessageInfo)obj;
            helper.setGone(R.id.muteView,true);
            helper.setGone(R.id.failView,true);
            helper.setGone(R.id.indicatorView,true);
            helper.setGone(R.id.tv_unread,true);

            UserAvatarView avatarView = helper.getView(R.id.iv_icon);
            avatarView.setTextSize(20);

            ChatInfo chat = TelegramManager.getTelegramManager().getTgData().chatInfo(messageInfo.chat_id);

            if (chat.isGroup()) {//群组
                //群组名称
                helper.setText(R.id.iv_title, chat.title);
                //群组头像
                avatarView.showGroup(chat);
            } else {//单聊
                //对方昵称
                helper.setText(R.id.iv_title, UserInfo.userDisplayName(chat.userId()));
                //
                UserInfo user = TelegramManager.getTelegramManager().getTgData().contactInfo(chat.userId());
                if (user != null) {
                    avatarView.showUser(user);
                } else {
                    avatarView.showName(UserInfo.userDisplayName(messageInfo.sender_user_id()));
                }
            }

            TextView timeText = helper.getView(R.id.tv_time);
            timeText.setText(DateHelper.getMessageFullDisplayTime(messageInfo.date * 1000, false));
            timeText.setCompoundDrawables(null, null, null, null);
            if (chat.isGroup()) {
                UserInfo user = UserInfo.getUserInfo(messageInfo.sender_user_id());
                if (user != null && !chat.lastMessage.isOutgoing() && !chat.lastMessage.isTipMessage()) {
                    helper.setText(R.id.tv_content, String.format("%s:%s", user.displayName(), messageInfo.toString()));
                } else {
                    helper.setText(R.id.tv_content, messageInfo.toString());
                }
            } else {
                helper.setText(R.id.tv_content, messageInfo.toString());
            }

        }
        if (obj instanceof ChatInfo) {
            ChatInfo chatInfo = (ChatInfo) obj;
            helper.setText(R.id.iv_title, chatInfo.title);
            UserInfo userInfo = UserInfo.getUserInfo(chatInfo.type.user_id);
            UserAvatarView avatarView = helper.getView(R.id.iv_icon);
            avatarView.setTextSize(17);
            avatarView.showUser(userInfo);
        }
    }

    @Override
    public @NotNull BaseLoadMoreModule addLoadMoreModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }
}
